package com.nineclock.system.service.impl;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.nineclock.common.constant.SMSConstant;
import com.nineclock.common.enums.ResponseEnum;
import com.nineclock.common.exception.NcException;
import com.nineclock.common.sms.AliyunSmsUtils;
import com.nineclock.common.utils.PhoneNumCheckUtils;
import com.nineclock.system.mapper.SysCompanyUserMapper;
import com.nineclock.system.pojo.SysCompanyUser;
import com.nineclock.system.service.SmsService;
import com.nineclock.system.service.SysCompanyUserService;
import com.nineclock.system.service.SysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class SmsServiceImpl implements SmsService {

    @Autowired
    SysCompanyUserMapper companyUserMapper;
    @Autowired
    RedisTemplate<String,String> redisTemplate;

    @Autowired
    SysCompanyUserService companyUserService;

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    AliyunSmsUtils smsUtils;
    @Override
    public void sendSmsCode(String mobile, String type) {
        //健壮性判断
        if (mobile == null || type==null) {
            throw new NcException(ResponseEnum.INVALID_PARAM_ERROR);
        }
        //判断手机号格式
        if (!PhoneNumCheckUtils.isPhoneLegal(mobile)) {
            throw new NcException(ResponseEnum.INVALID_PHONE_NUMBER);
        }
        //通过手机号查询员工信息
        /*LambdaQueryWrapper<SysCompanyUser> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(SysCompanyUser::getMobile,mobile);
        List<SysCompanyUser> users = companyUserMapper.selectList(wrapper);
        */
        Boolean flag = sysUserService.checkExists(mobile);
        //如果是注册, 判断手机是否已经存在 , 如果存在, 则不允许注册;
        if ("2".equals(type) && flag){
//            String randomCode = RandomUtil.randomNumbers(6);
//            log.info("验证码:"+randomCode);
//            String key = getPrefix(type) + mobile;
//            redisTemplate.boundValueOps(key).set(randomCode,5, TimeUnit.MINUTES);

            throw new NcException(ResponseEnum.USER_MOBILE_EXISTS);
        }
        //其他情况 手机号必须存在
        if (!"2".equals(type) && !flag) {
            throw new NcException(ResponseEnum.USER_NOT_REGISTER);
        }
        String randomCode = RandomUtil.randomNumbers(6);

//        Boolean result = smsUtils.sendSMS(mobile, randomCode);
//        if(!result){
//            throw new NcException(ResponseEnum.SEND_MESSAGE_ERROR);
//        }
        log.info("验证码:"+randomCode);
        String key = getPrefix(type) + mobile;
        redisTemplate.boundValueOps(key).set(randomCode,5, TimeUnit.MINUTES);

    }

    @Override
    public Boolean verify(String mobile, String type, String checkcode) {
        if (mobile == null|| type == null || checkcode == null ) {
            throw new NcException(ResponseEnum.INVALID_PARAM_ERROR);
        }
        String prefix = getPrefix(type);
        String realCode = redisTemplate.boundValueOps(prefix + mobile).get();
        if (!StrUtil.isNotEmpty(realCode)){
            throw new NcException(ResponseEnum.DATA_NOT_EXISTS);
        }
        if (realCode.equals(checkcode)) {
            return true;
        }
        return false;
    }





    public String getPrefix(String type){
        switch (type){
            case SMSConstant.SMS_TYPE_LOGIN:
                return SMSConstant.SMS_LOGIN_KEY_PREFIX;
            case SMSConstant.SMS_TYPE_REGISTER:
                return SMSConstant.SMS_REGISTER_KEY_PREFIX;
            case SMSConstant.SMS_TYPE_CHANGE_MANAGER:
                return SMSConstant.SMS_RESET_KEY_PREFIX;
            case SMSConstant.SMS_TYPE_RESET_PASSWORD:
                return SMSConstant.SMS_CHANGE_MANAGER_KEY_PREFIX;
            default:
                return null;
        }
    }
}